package com.zcy.gift.strategy.impl;

import com.zcy.gift.strategy.IShopCarDataSyncKeysGroupStrategy;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author lucker
 * @date 2024/2/22 11:40
 */
public class ShopCarDataSyncKeysGroupByOrder implements IShopCarDataSyncKeysGroupStrategy {
    private int groupNum = 100;

    public ShopCarDataSyncKeysGroupByOrder() {

    }

    public ShopCarDataSyncKeysGroupByOrder(int groupNum) {
        this.groupNum = groupNum;
    }

    @Override
    public Map<Integer, List<String>> group(Set<String> keys) {
        List<String> keyList = new ArrayList<>(keys);
        Collections.sort(keyList);
        Map<Integer, List<String>> map = new HashMap<>();
        for (int i = 0; i < keyList.size(); i++) {
            int index = i % this.groupNum;
            if (map.containsKey(index)) {
                map.get(index).add(keyList.get(i));
            } else {
                List<String> list = new ArrayList<>();
                list.add(keyList.get(i));
                map.put(index, list);
            }
        }
        return map;
    }
}
